package com.sitechasia.webx.components.xtable.toolbar;

import java.util.Iterator;

import org.extremecomponents.table.bean.Export;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.TableActions;
import org.extremecomponents.table.view.html.TwoColumnRowLayout;
import org.extremecomponents.table.view.html.toolbar.ImageItem;
import org.extremecomponents.table.view.html.toolbar.TextItem;
import org.extremecomponents.table.view.html.toolbar.ToolbarItemUtils;
import org.extremecomponents.util.HtmlBuilder;

import com.sitechasia.webx.components.xtable.util.ExtremeUtils;

/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：列表工具条
*
* @author kuanglingxuan
* Created on 2007-8-1
*/
public class MyToolBar extends TwoColumnRowLayout {
	public MyToolBar(HtmlBuilder html, TableModel model) {
		super(html, model);
	}

	protected boolean showLayout(TableModel model) {
		boolean showStatusBar = BuilderUtils.showStatusBar(model);
		boolean filterable = BuilderUtils.filterable(model);
		boolean showExports = BuilderUtils.showExports(model);
		boolean showPagination = BuilderUtils.showPagination(model);
		boolean showTitle = BuilderUtils.showTitle(model);
		if (!showStatusBar && !filterable && !showExports && !showPagination && !showTitle) {
			return false;
		}
		return true;
	}

	protected void columnLeft(HtmlBuilder html, TableModel model) {
		boolean showStatusBar = BuilderUtils.showStatusBar(model);
		if (!showStatusBar) {
			return;
		}
	}

	/**
	 * 扩展表头添加具体页数
	 */
	protected void columnRight(HtmlBuilder html, TableModel model) {
		boolean showPagination = BuilderUtils.showPagination(model);
		boolean showExports = BuilderUtils.showExports(model);
		CustomToolbarBuilder toolbarBuilder = new CustomToolbarBuilder(html, model);
		Integer rowDisplayed = (Integer)model.getContext().getRequestAttribute("ec_crd");
		String selectStr = model.getPreferences().getPreference("table.rowsDisplayed.select");
//		System.out.println("selectStr-->"+selectStr);
//		System.out.println("rowDisplayed-->"+rowDisplayed);
		if(selectStr != null && rowDisplayed != null){
			String[] selectArray = selectStr.split(",");
			html.td(2).align("left").styleClass("bg_listPageNum").close();
			html.append(model.getMessages().getMessage("toolbar.text.rowsDisplayed"));
			html.append("<select onchange=\"javascript:document.getElementById('ec_crd').value = this.value;");
			html.append("document.forms." + BuilderUtils.getForm(model) + ".submit();\">");
			for(int i=0;i<selectArray.length;i++){
//				System.out.println("selectArray[i]-->"+selectArray[i]);
				if(selectArray[i].equals(rowDisplayed.toString())){
					html.append("<option selected value='"+selectArray[i]+"'>"+selectArray[i]+"</option>");
				}else
					html.append("<option value='"+selectArray[i]+"'>"+selectArray[i]+"</option>");
			}
			html.append("</select>");
			html.append(model.getMessages().getMessage("toolbar.text.row"));
			html.tdEnd();
		}
		html.td(2).align("right").styleClass("bg_listPageNum").close();
		html.table(2).border("0").cellPadding("0").cellSpacing("0").styleClass(BuilderConstants.TOOLBAR_CSS).close();
		html.tr(3).styleClass(" height:35px;line-height:35px;").close();
		if (showPagination) {
			html.td(4).close();
			TextItem item1 = new TextItem();

			item1.setText(model.getMessages().getMessage("toolbar.text.firstPage"));
			item1.setStyleClass("common_link_word");
			ToolbarItemUtils.buildFirstPage(html, model, item1);
			html.tdEnd();
			html.td(4).close();
			html.append("&nbsp;&nbsp;");
			html.tdEnd();
			html.td(4).close();
			TextItem item2 = new TextItem();

			item2.setText(model.getMessages().getMessage("toolbar.text.prevPage"));
			item2.setStyleClass("common_link_word");
			ToolbarItemUtils.buildPrevPage(html, model, item2);
			html.tdEnd();
			html.td(4).close();
			html.append("&nbsp;&nbsp;");
			// toolbarBuilder.separator();
			html.tdEnd();
			html.td(4).close();
			TextItem item3 = new TextItem();
//			item3.setTooltip(model.getMessages().getMessage("toolbar.text.nextPage"));//"下一页");
//			System.out.println("page--->"+model.getLimit().getPage());
//			System.out.println("rows--->"+model.getLimit().getTotalRows());
			item3.setText(model.getMessages().getMessage("toolbar.text.nextPage"));
			item3.setStyleClass("common_link_word");
			ToolbarItemUtils.buildNextPage(html, model, item3);
			html.tdEnd();
			html.td(4).close();
			html.append("&nbsp;&nbsp;");
			html.tdEnd();
			html.td(4).close();
			TextItem item4 = new TextItem();
			item4.setStyleClass("common_link_word");
			item4.setText(model.getMessages().getMessage("toolbar.text.lastPage"));
			ToolbarItemUtils.buildLastPage(html, model, item4);
			html.tdEnd();
			html.td(4).close();
			html.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			// toolbarBuilder.separator();
			html.tdEnd();
			html.td(4).close();
			html.newline();
//			System.out.println("getCurrentRowsDisplayed--->"+model.getLimit().getCurrentRowsDisplayed());
//			System.out.println("getTotalRows--->"+model.getLimit().getTotalRows());
			int totalPages = 1;
//			realTotalRows真实记录条数，可以看ExtremeUtils类addEmptyRows方法解释
			Integer total = (Integer)model.getContext().getRequestAttribute("realTotalRows");//model.getLimit().getTotalRows());

			if(total != null && rowDisplayed != null)
				if(total > rowDisplayed)
					totalPages = BuilderUtils.getTotalPages(model);

			int currentPage = model.getLimit().getPage();
			html.append(model.getMessages().getMessage("toolbar.text.curPage")+"<span class=\"pageNum\">" + currentPage + "/" + totalPages + "</span>"+model.getMessages().getMessage("toolbar.text.page"));
			html.tdEnd();
			html.td(4).close();
			html.newline();
			html.append("&nbsp;&nbsp;");
			html.tdEnd();
			html.td(4).close();
//			System.out.println("model.getLimit().getTotalRows()1---->"+model.getLimit().getTotalRows());


			html.append(model.getMessages().getMessage("toolbar.text.total")+"<span class=\"pageNum\">" + total + "</span>"+model.getMessages().getMessage("toolbar.text.row"));

//			System.out.println("model.getLimit().getTotalRows()2---->"+model.getLimit().getTotalRows());
			html.tdEnd();
			html.td(4).close();
			html.append("&nbsp;&nbsp;");
			html.append(model.getMessages().getMessage("toolbar.text.jumpTo"));
			html.tdEnd();
			html.td(4).width("30").close();
			html.input("text").styleClass("inputtext").id("textNum").align("right").name("pages1").size("1").value("1").maxlength("3").style("width:100%")
					.xclose();

//			System.out.println("BuilderUtils.getForm(model)---->"+BuilderUtils.getForm(model));
			html.append("<script type=\"text/javascript\">document.getElementById(\"textNum\").value=document.forms."+BuilderUtils.getForm(model)+".ec_p.value;</script>");
			html.append("<script type=\"text/javascript\">document.getElementById(\"ec_p\").value='1';</script>");
			html.tdEnd();
			html.td(4).close();
			html.append(model.getMessages().getMessage("toolbar.text.page"));
			html.tdEnd();

			html.append(" ");

			ResItem goItem = new ResItem();
			goItem.setText(model.getMessages().getMessage("toolbar.text.go"));
			toolbarBuilder.buildCurPage(html, model, goItem);
//			html.tdEnd();
//			if (showExports) {
//				html.td(4).close();
//				html.append("&nbsp;&nbsp;");
//				// toolbarBuilder.separator();
//				html.tdEnd();
//			}
		}
//		if (showExports) {
//			Iterator iterator = model.getExportHandler().getExports().iterator();
//			for (Iterator iter = iterator; iter.hasNext();) {
//				html.td(4).close();
//				Export export = (Export) iter.next();
//				toolbarBuilder.exportItemAsImage(export);
//				html.tdEnd();
//			}
//		}
		html.trEnd(3);
		html.tableEnd(2);
		html.tdEnd();
	}
}
